package com.javaweb.service.impl;

import com.javaweb.common.BusinessConstants;
import com.javaweb.dao.ArticleDao;
import com.javaweb.dao.UserDao;
import com.javaweb.model.Article;
import com.javaweb.service.ArticleService;
import com.javaweb.service.dto.ArticleConditionDto;
import com.javaweb.service.dto.ArticleUpdateDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/10/16 9:47
 */

@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private UserDao userDao ;
    @Autowired
    private ArticleDao articleDao ;

    public void method() {
        this.publishArticle("1","1") ;
    }


    @Transactional
//            (
//            propagation = Propagation.REQUIRED,
//            isolation = Isolation.DEFAULT,
//            timeout = -1,
//            readOnly = true,
//            rollbackFor = Throwable.class
//    )
    @Override
    public int publishArticle(String userId, String articleId) {

        Integer ret =  articleDao.updateStatus(Integer.valueOf(articleId),
                               BusinessConstants.ARTICLE_PUBLISHED) ;
        if (ret > 0) {
            ret = userDao.updateScore(Integer.valueOf(userId),
                    BusinessConstants.ARTICLE_SCORE) ;
        } else {
            log.error("publish article : update status failed." ) ;
            return 0 ;
        }
        try {
            if (true) {
                throw new RuntimeException("exception.");
            }
        } catch (Exception e) {
            log.error(e.getMessage()) ;
        }
        log.info("publish article successfully." ) ;
        return ret;
    }

    @Override
    public List<Article> queryByArticleCondition(ArticleConditionDto articleCond) {
        return articleDao.selectArticles(articleCond);
    }

    @Override
    public Integer addArticle(Article article) {
        article.setCreateTime(new Date());
        article.setUpdateTime(new Date());
        return articleDao.insert(article);
    }

    @Override
    public Integer updateArticle(ArticleUpdateDto articleUpdateDto) {
        return articleDao.updateArticle(articleUpdateDto);
    }
}
